package com.cnlbc.View;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import com.cnlbc.Dao.BookDao;
import com.cnlbc.Dao.BookTypeDao;
import com.cnlbc.Model.Book;
import com.cnlbc.Model.BookType;
import com.cnlbc.Util.DbUtil;
import com.cnlbc.Util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class Addbook extends JInternalFrame {
	private JTextField bookName;
	private JTextField anthor;
	private JTextField bookPrice;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JComboBox bookType;
	private JTextArea bookDesc;
	private JRadioButton man;
	private JRadioButton woman;
	
	private DbUtil dbUtil=new DbUtil();
	private BookTypeDao bookTypedao=new BookTypeDao();
	private BookDao bookDao=new BookDao();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Addbook frame = new Addbook();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Addbook() {
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 536, 501);
		
		JLabel lblNewLabel = new JLabel("图书名称：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookName = new JTextField();
		bookName.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("图书作者：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
		
		anthor = new JTextField();
		anthor.setFont(new Font("宋体", Font.PLAIN, 18));
		anthor.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("作者性别：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
		
		JLabel lblNewLabel_3 = new JLabel("图书价格：");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookPrice = new JTextField();
		bookPrice.setFont(new Font("宋体", Font.PLAIN, 18));
		bookPrice.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("图书类别：");
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 16));
		
		JLabel lblNewLabel_5 = new JLabel("图书描述：");
		lblNewLabel_5.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookDesc = new JTextArea();
		bookDesc.setFont(new Font("Monospaced", Font.PLAIN, 16));
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addBookAction(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(Addbook.class.getResource("/images/add.png")));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(Addbook.class.getResource("/images/reset.png")));
		
		man = new JRadioButton("男");
		man.setFont(new Font("宋体", Font.PLAIN, 16));
		man.setSelected(true);
		buttonGroup.add(man);
		
		woman = new JRadioButton("女");
		woman.setFont(new Font("宋体", Font.PLAIN, 16));
		buttonGroup.add(woman);
		
		bookType = new JComboBox();
		bookType.setFont(new Font("宋体", Font.PLAIN, 16));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(86)
					.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
					.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
					.addGap(88))
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(34)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_5)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookDesc, GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel_4)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(bookType, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE))
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel_2)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(man)
											.addGap(18)
											.addComponent(woman)))
									.addPreferredGap(ComponentPlacement.RELATED, 218, Short.MAX_VALUE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(35)
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookName, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(anthor))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_3)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addGap(4)))
					.addGap(31))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(73)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblNewLabel)
										.addComponent(bookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(77)
									.addComponent(lblNewLabel_1)))
							.addGap(36)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_3)
								.addComponent(bookPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(woman)
								.addComponent(man))
							.addGap(34)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_4)
								.addComponent(bookType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(74)
							.addComponent(anthor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_5)
						.addComponent(bookDesc, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(37, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		bookDesc.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
		fillBookType();
	}
	/**
	 * 添加图书
	 * @param evt
	 */
	private void addBookAction(ActionEvent evt) {
		String Bookname=this.bookName.getText();
		String Anthor=this.anthor.getText();
		String Bookprice=this.bookPrice.getText();
		String Bookdesc=this.bookDesc.getText();
		if(StringUtil.isEmpty(Bookname)) {
			JOptionPane.showMessageDialog(null, "图书名称不能为空");
			return;
		}
		if(StringUtil.isEmpty(Anthor)) {
			JOptionPane.showMessageDialog(null, "图书作者不能为空");
			return;
		}
		if(StringUtil.isEmpty(Bookprice)) {
			JOptionPane.showMessageDialog(null, "图书价格不能为空");
			return;
		}
		String sex="";
		if(man.isSelected()) {
			sex="男";
		}else {
			sex="女";
		}
		
		BookType booktype=(BookType)bookType.getSelectedItem();
		int booktypeId=booktype.getId();
		
		Book book=new Book(Bookname, Anthor, sex, Float.parseFloat(Bookprice),booktypeId, Bookdesc);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int addNumber=bookDao.addBook(con, book);
			if(addNumber==1) {
				JOptionPane.showMessageDialog(null, "添加成功");
				reset();
			}else {
				JOptionPane.showMessageDialog(null, "添加失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "添加失败");
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 重置
	 */
	private void reset() {
		// TODO Auto-generated method stub
		this.bookName.setText("");
		this.anthor.setText("");
		this.man.setSelected(true);
		this.bookPrice.setText("");
		this.bookDesc.setText("");
		if(this.bookType.getItemCount()>0) {
			this.bookType.setSelectedIndex(0);
		}
	}

	/**
	 * 初始化下拉框
	 */
	private void fillBookType() {
		Connection con=null;
		BookType booktype=new BookType();
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookTypedao.list(con, new BookType());
			while(rs.next()) {
				booktype=new BookType();
				booktype.setId(rs.getInt("id"));
				booktype.setBookTypeName(rs.getString("bookTypeName"));
				this.bookType.addItem(booktype);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
